//
//  DataManager.m
//  XDLBaseProject
//
//  Created by 谢东来 on 2019/9/8.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "DataManager.h"

@interface DataManager()<NSCopying,NSMutableCopying>
@end

static DataManager *manager = nil;

@implementation DataManager

+ (instancetype)shareManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
        manager.dataModel = [[DataModel alloc] init];
    });
    return manager;
}

//获得JS的下载路径
- (NSString *)getJSSavePath {
    NSFileManager * fileManager = [NSFileManager defaultManager];
    
    NSString * finderPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                  NSUserDomainMask, YES) lastObject]
                             stringByAppendingPathComponent:@"JS"];
    
    if (![fileManager fileExistsAtPath:finderPath]) {
        [fileManager createDirectoryAtPath:finderPath withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return [finderPath stringByAppendingPathComponent:@"f5stego.js"];
}

//获得已嵌入内容的图片的下载路径
- (NSString *)getCoverImagesSavePath {
    NSFileManager * fileManager = [NSFileManager defaultManager];
    
    NSString * finderPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                  NSUserDomainMask, YES) lastObject]
                             stringByAppendingPathComponent:@"DOWNLOADCOVER"];
    
    if (![fileManager fileExistsAtPath:finderPath]) {
        [fileManager createDirectoryAtPath:finderPath withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return [finderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", [PublicMethod uuid]]];
}

//存儲數據
- (void)saveData {
    // 获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"dataModel.arch"];
    // 保存數據对象
    [NSKeyedArchiver archiveRootObject:self.dataModel toFile:path];
}

//存儲臨時數據
- (void)saveTempData {
    // 获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.arch",self.dataModel.zhanghao]];
    // 保存數據对象
    [NSKeyedArchiver archiveRootObject:self.dataModel toFile:path];
}

//讀取數據
- (void)readingData {
    // 获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"dataModel.arch"];
    // 解档
    self.dataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

//讀取臨時數據
- (void)readingTempDataWithID:(NSString *)str {
    // 获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.arch",str]];
    // 解档
    self.tempDataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

//清空臨時數據
- (void)clearTempDataWithID:(NSString *)str {
    // 获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.arch",str]];
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
    if (exists) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [super allocWithZone:zone];
        manager.dataModel = [[DataModel alloc] init];
    });
    return manager;
}

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return manager;
}

- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
    return manager;
}

@end
